package router

import (
	"github.com/gin-gonic/gin"
	"zk507/api/client"
	"zk507/api/pkg"
	"zk507/api/request"
	"zk507/api/response"
	"zk507/common/proto/user"
)

func UserLogin(c *gin.Context) {
	var data request.UserLoginRequest
	if err := c.ShouldBind(&data); err != nil {
		response.ResponseError(c, "错误", err)
		return
	}
	res, err := client.UserLogin(c, &user.UserLoginRequest{
		Mobile: data.Mobile,
		Code:   data.Code,
	})
	if err != nil {
		response.ResponseError(c, err.Error(), nil)
		return
	}
	claims := pkg.CustomClaims{ID: uint(res.UserId)}
	token, err := pkg.NewJWT("qq").CreateToken(claims)
	if err != nil {
		response.ResponseError(c, "token加密失败", nil)
		return
	}
	response.ResponseSuccess(c, "登录成功", token)

}
func UserSms(c *gin.Context) {
	var data request.UserSmsRequest
	if err := c.ShouldBind(&data); err != nil {
		response.ResponseError(c, "错误", err)
		return
	}
	res, err := client.Sms(c, &user.SmsRequest{
		Mobile: data.Mobile,
		Source: data.Source,
	})
	if err != nil {
		response.ResponseError(c, err.Error(), nil)
		return
	}
	response.ResponseSuccess(c, "短信发送成功", res.Success)

}
func InformationList(c *gin.Context) {
	var data request.InformationListRequest
	if err := c.ShouldBind(&data); err != nil {
		response.ResponseError(c, "错误", err)
		return
	}
	res, err := client.InformationList(c, &user.InformationListRequest{
		Address: data.Address,
		Scale:   data.Scale,
	})
	if err != nil {
		response.ResponseError(c, err.Error(), nil)
		return
	}
	response.ResponseSuccess(c, "查询成功", res.List)

}
func InformationSynchronous(c *gin.Context) {
	res, err := client.InformationSynchronous(c, &user.InformationSynchronousRequest{})
	if err != nil {
		response.ResponseError(c, err.Error(), nil)
		return
	}
	response.ResponseSuccess(c, "企业信息存储redis成功", res.Success)

}
